<template>
  <div class="operate-button-container">
    <el-row>
      <el-col :span="3">
        <el-button
          @click="handleCreate"
          icon="el-icon-plus"
          plain
          round
          size="small"
          type="primary"
          v-if="buttonDisplayRange === 'manage'"
        >新增</el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          @click="handleEdit"
          icon="el-icon-edit"
          plain
          round
          size="small"
          type="info"
          v-if="buttonDisplayRange === 'manage'"
        >修改</el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          @click="handleDelete"
          icon="el-icon-delete"
          plain
          round
          size="small"
          type="danger"
          v-if="buttonDisplayRange === 'manage'"
        >删除</el-button>
      </el-col>
      <el-col :span="3">
        <el-button
          @click="handleRefSelect"
          icon="el-icon-"
          plain
          round
          size="small"
          type="success"
          v-if="buttonDisplayRange === 'reference'"
        >确定</el-button>
      </el-col>
    </el-row>

    <el-dialog :visible.sync="dialogFormVisible" title="新增用户分组" >
      <userGroup-form
        :dataId="dataId"
        :dialogFormVisible="dialogFormVisible"
        @handleDialog="handleDialog"
      ></userGroup-form>
    </el-dialog>
  </div>
</template>

<script>
import UserGroupForm from '@/views/business/userGroup/userGroupForm'
export default {
  name: 'OperateButton',
  components: {
    UserGroupForm
  },
  props: {
    buttonDisplayRange: String,
    multipleSelection: Array,
    refSelectType: String
  },
  data() {
    return {
      dataId: null,
      dialogFormVisible: false
    }
  },
  methods: {
    handleDialog() {
      this.dialogFormVisible = false
      this.$emit('handleSearch')
    },
    handleRefSelect() {
      if (this.refSelectType === 'radio') {
        if (this.multipleSelection.length == 1) {
          this.$emit('handleRefSelect', this.multipleSelection)
          //清空选择框
          this.$parent.$refs.paginationTable.toggleSelection()
        } else {
          this.$message.error('只能勾选一条记录')
        }
      } else if (this.refSelectType === 'multiple') {
        if (this.multipleSelection.length >= 1) {
          this.$emit('handleRefSelect', this.multipleSelection)
          //清空选择框
          this.$parent.$refs.paginationTable.toggleSelection()
        } else {
          this.$message.error('至少勾选一条记录')
        }
      }
    },
    handleCreate() {
      this.dataId = null
      this.dialogFormVisible = true
    },
    handleEdit() {
      if (this.multipleSelection.length == 1) {
        this.dataId = this.multipleSelection[0].id
        this.dialogFormVisible = true
      } else {
        this.$message.error('请勾选一条记录进行修改！')
      }
    },
    handleDelete() {
      if (this.multipleSelection.length == 1) {
        this.$confirm('是否删除此记录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var dataId = this.multipleSelection[0].id
            this.$store.dispatch('userGroup/deleteData', dataId).then(() => {
              this.$emit('handleSearch')
            })
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.$message.error('最多只能删除一条记录')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

